#include <stdio.h>
#include <stdlib.h>

typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
    ElementType Data;
    PtrToNode   Next;
};
typedef PtrToNode List;

List Read(); /* 细节在此不表 */
void Print( List L ); /* 细节在此不表；空链表将输出NULL */

List Merge( List L1, List L2 );

int main()
{
    List L1, L2, L;
    L1 = Read();
    L2 = Read();
    L = Merge(L1, L2);
    Print(L);
    Print(L1);
    Print(L2);
    return 0;
}

/* 你的代码将被嵌在这里 */
List Read(void)
{
    int num,i;
    List p = (List)malloc(sizeof(struct Node));
    List temp = p;
    List a = NULL;

    scanf("%d",&num);
    for(i = 1; i <= num; i++)
    {
        List a = (List)malloc(sizeof(struct Node));
        scanf("%d",&a->Data);
        a->Next = NULL;
        temp->Next = a;
        temp = temp->Next;
    }
    return p;
}
void Print(List L)
{
    List temp = L;
    
    if(!L)
    {
        printf("NULL");
        exit(0);
    }
    while(temp)
    { 
        if(temp->Next != NULL)
            printf("%d",temp->Data);

        temp = temp->Next;
    }
}
List Merge( List L1, List L2 )
{
    List pa, pb, pc, L;
    L = (List)malloc(sizeof(struct Node)); //申请一个空节点，注意它不是头结点，它在头结点的前面
    pc = L;
    pa = L1->Next;  //因为第一个节点为空，必须从头结点开始比较大小
    pb = L2->Next;
    while(pa&&pb)
    {
        if(pa->Data <= pb->Data) 
        {
            pc->Next = pa;  //从上一个较小的节点指向这个较小的节点
            pc = pa; //每次将pc移动到这个较小的节点
            pa = pa->Next;
        }else{
            pc->Next = pb;
            pc = pb;
            pb = pb->Next;
        }
    }

    pc->Next = pa?pa:pb;
    L1->Next = NULL;
    L2->Next = NULL;
    return L;
}
